package com.zzwtec.information.controller;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.zzwtec.information.model.Role;
import com.zzwtec.information.dao.RoleDao;
import com.zzwtec.interfaces.ResultObject;

import com.zzwtec.common.util.BeanUtil;
import com.zzwtec.common.util.ErrorMsg;
import com.zzwtec.interfaces.Page;
import com.zzwtec.interfaces.RequestObject;
import com.zzwtec.interfaces.information.dto.RoleDTO;
import com.zzwtec.interfaces.information.services.RoleService;


/**
 * 微服务RoleService接口实现类
 * @author  草原狼
 * 
 * 2017年08月07日
 */

@RestController
public class RoleController implements RoleService {
	
	@Autowired
	private DiscoveryClient discoveryClient;

	@Resource(name = "roleDao")
	private RoleDao roleDao;

	@Override
	public ResultObject<String> addRole(@RequestBody RequestObject<RoleDTO> request) {
		ResultObject<String> result = new ResultObject<String>(request);
		RoleDTO dto = request.getData();
		Role bean = new Role();
		BeanUtil.copyProperties(bean, dto);		
		roleDao.insert(bean);
		result.setData(bean.getId());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
		
	}
	
	@Override
	public ResultObject<Void> removeRole(@RequestBody RequestObject<String> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		roleDao.deleteById(request.getData());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}
	
	@Override
	public ResultObject<Void> removeRoleByIds(RequestObject<String[]> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		roleDao.deleteByIds(request.getData());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	

	@Override
	public ResultObject<Void> modifyRole(@RequestBody RequestObject<RoleDTO> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		RoleDTO dto = request.getData();
		Role bean = new Role();
		BeanUtil.copyProperties(bean, dto);		
		roleDao.update(bean);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
		
	}


	@Override
	public ResultObject<List<RoleDTO>> queryAllRole(@RequestBody RequestObject<Void> request) {
		ResultObject<List<RoleDTO>> result = new ResultObject<List<RoleDTO>>(request);
		List<Role> list = roleDao.selectAll();
		List<RoleDTO> data = new ArrayList<RoleDTO>();
		for(Role entity : list){
			RoleDTO dto = new RoleDTO();
			BeanUtil.copyProperties(dto, entity);
			data.add(dto);
		}
		result.setData(data);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}


	@Override
	public ResultObject<RoleDTO> queryRoleById(@RequestBody RequestObject<String> request) {
		ResultObject<RoleDTO> result = new ResultObject<RoleDTO>(request);
		Role entity = roleDao.selectOne(request.getData());
		RoleDTO dto = new RoleDTO();
		BeanUtil.copyProperties(dto, entity);
		result.setData(dto);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}

	@Override
	public ResultObject<Page<RoleDTO>> queryRoleByPage(@RequestBody RequestObject<Page<RoleDTO>> request) {
		ResultObject<Page<RoleDTO>>  result=new ResultObject<Page<RoleDTO>>(request);
		Page<RoleDTO> page=request.getData();
		Page<Role> rPage=new Page<Role>();
		rPage.setCondition(page.getCondition());
		rPage.setDistinct(page.isDistinct());
		rPage.setGroupByFields(page.getGroupByFields());
		rPage.setOrderField(page.getOrderField());
		rPage.setOrderType(page.getOrderType());
		rPage.setPage(page.getPage());
		rPage.setPageNum(page.getPageNum());
		rPage.setSearchFields(page.getSearchFields());
		rPage.setSearchValues(page.getSearchValues());
		rPage = roleDao.selectPage(rPage);
		if(rPage.getData()!=null){
			List<RoleDTO> data = new ArrayList<RoleDTO>(rPage.getData().size());
			for(Role entity : rPage.getData()){	
				RoleDTO dto = new RoleDTO();
				BeanUtil.copyProperties(dto, entity);
				data.add(dto);
			}
			page.setData(data);
		}
		page.setTotal(rPage.getTotal());
		page.setRows(rPage.getRows());
		result.setData(page);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	
	
	@Override
	public ServiceInstance showServiceInfo() {		
		ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}
}
